
#!/bin/bash

# 更新系统软件包
sudo yum update -y

# 安装依赖软件包
sudo yum install -y epel-release
sudo yum install -y nginx gcc-c++ make openssl-devel pcre-devel zlib-devel curl-devel perl-devel perl-ExtUtils-Embed libxslt-devel gd-devel geoip-devel

# 下载Nginx源码
NGINX_VERSION=1.21.3
wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz
tar -zxvf nginx-$NGINX_VERSION.tar.gz
cd nginx-$NGINX_VERSION

# 配置编译选项，包括Lua模块支持
./configure --with-http_ssl_module --with-http_geoip_module --with-http_realip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_v2_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-compat --with-http_addition_module --with-http_xslt_module=dynamic --with-http_xslt_module=dynamic --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-stream_geoip_module=dynamic --with-http_realip_module --add-module=../ngx_devel_kit --add-module=../lua-nginx-module

# 编译并安装Nginx
make
sudo make install

# 创建Nginx服务
sudo tee /etc/systemd/system/nginx.service <<EOF
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
# 启动Nginx服务
sudo systemctl start nginx

# 设置Nginx开机自启
sudo systemctl enable nginx

# 配置HTTPS支持
sudo yum install -y certbot python3-certbot-nginx
sudo certbot --nginx

# 配置完成，重启Nginx以应用更改
sudo systemctl restart nginx

# 清理安装过程中的临时文件
cd ..
rm -rf nginx-$NGINX_VERSION nginx-$NGINX_VERSION.tar.gz

echo "Nginx已成功安装并配置完成。"
